package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TopNameVO;
import com.sky.vo.TurnoverStatisticsVO;
import com.sky.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;


@RestController
@Slf4j
@Api(tags = "报表管理")
@RequestMapping("/admin/report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @ApiOperation("營業額統計")
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverStatisticsVO> turnoverStatistics
            (@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        TurnoverStatisticsVO turnoverStatisticsVO = reportService.turnoverStatistics(begin, end);
        return Result.success(turnoverStatisticsVO);
    }

    @ApiOperation("用戶統計接口")
    @GetMapping("/userStatistics")
    public Result<UserVO> userStatistics
            (@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        UserVO userVO = reportService.userStatistics(begin, end);
        return Result.success(userVO);
    }

    @ApiOperation("訂單統計接口")
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        OrderReportVO orderReportVO = reportService.ordersStatistics(begin, end);
        return Result.success(orderReportVO);
    }

    @ApiOperation("查詢銷量排名top10")
    @GetMapping("/top10")
    public Result<TopNameVO> top10(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        TopNameVO topNameVO = reportService.top10(begin, end);
        return Result.success(topNameVO);
    }

    @ApiOperation("導出Excel報表接口")
    @GetMapping("/export")
    public Result export(HttpServletResponse response) {
        reportService.export(response);
        return Result.success();
    }
}
